package com.huike.review.controller;

import com.huike.review.pojo.Review;
import com.huike.review.service.impl.ReviewServiceImpl;
import com.huike.review.vo.MybatisReviewVO;
import com.huike.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController //这是 controller必须要有的注解 用@Controller 也可以 但是只返回json格式数据的 controller 最好用 @RestController
@RequestMapping("/review")//这是spring 请求路径的注解
public class ReviewController {
    @Autowired
    ReviewServiceImpl reviewService;

    @GetMapping("saveData/{name}/{age}/{sex}")
    public JsonResult<Object> saveDataUseGet(@PathVariable String name,@PathVariable Integer age,@PathVariable String sex) {

        String code;
        String msg;
        try {
            Review review = new Review();
            review.setName(name);
            review.setAge(age);
            review.setSex(sex);
            reviewService.saveData(review);
            code = "200";
            msg = "成功插入:1条数据";
        } catch (Exception exception) {
            code = "404";
            msg = "保存失败!";
        }
        return new JsonResult<>(code, msg);
    }

    @PostMapping("saveData")
    public JsonResult<Object> saveData(@RequestBody Review review) {

        String code;
        String msg;
        try {
            reviewService.saveData(review);
            code = "200";
            msg = "成功插入:1条数据";
        } catch (Exception exception) {
            code = "404";
            msg = "保存失败!";
        }
        return new JsonResult<>(code, msg);
    }

    @PostMapping("update")//这是spring 接收post请求的注解
    public JsonResult<Object> update(@RequestBody Review review) {
        String code;
        String msg;
        try {
            int num = reviewService.update(review);

            if (num == 0) {
                code = "400";
                msg = "修改失败,id为:" + review.getId() + "的人员不存在!";
            } else {
                code = "200";
                msg = "修改成功";
            }
        } catch (Exception exception) //捕获异常 Exception类是所有异常的父类 ,这句话意思是捕获所有异常
        {
            code = "404";
            msg = "发生错误,修改失败!";
        }
        return new JsonResult<>(code, msg);
    }

    @DeleteMapping("remove/{id}")
    public JsonResult<Object> remove(@PathVariable int id) {
        String code;
        String msg;
        try {
            int num = reviewService.remove(id);

            if (num == 0) {
                code = "400";
                msg = "删除失败,id为:" + id + "的人员不存在!";
            } else {
                code = "200";
                msg = "成功删除:1条数据";
            }
        } catch (Exception exception) //捕获异常 Exception类是所有异常的父类 ,这句话意思是捕获所有异常
        {
            code = "404";
            msg = "发生错误,删除失败!";
        }
        return new JsonResult<>(code, msg);
    }

    @GetMapping("getDataByPage")
    public JsonResult<Object> getDataByPage(int pageNum,int pageSize) {

        String code;
        String msg;
        MybatisReviewVO reviewVO = null;
        try {
            HashMap map = new HashMap();
           Integer pageMin = (pageNum-1)*pageSize;

            map.put("pageMin",pageMin);
            map.put("pageSize", pageSize);
            reviewVO = reviewService.getDataByPage(map);
            code = "200";
            msg = "查询成功";
       } catch (Exception exception) {
            code = "404";
            msg = "查询失败!";
       }
        return new JsonResult<>(reviewVO, code, msg);
    }

    @GetMapping("getById")
    public JsonResult<Review> getById(int id) {

        String code;
        String msg;

        Review review = null;
        try {

            review = reviewService.getById(id);
            code = "200";
            msg = "操作成功";
        } catch (Exception exception) {
            code = "404";
            msg = "查询失败!";
        }
        return new JsonResult<>(review, code, msg);
    }
}

